#include "StringConversion.h"

StringConversion::StringConversion(LPSTR str, INT len/*= -1*/, UINT type/*=CP_ACP*/)
{
	bWstr = TRUE;
	pCstr = str;
	nLen = len;
	nType = type;
	nChangeLen = ::MultiByteToWideChar(nType, 0, str, len, NULL, 0);
	pWstr = (LPWSTR)::HeapAlloc(::GetProcessHeap(), HEAP_ZERO_MEMORY, nChangeLen);
}

StringConversion::StringConversion(LPWSTR str, INT len /*= -1*/, UINT type /*= CP_ACP*/)
{
	bWstr = FALSE;
	pWstr = str;
	nLen = len;
	nType = type;
	nChangeLen = ::WideCharToMultiByte(nType, 0, pWstr, nLen, NULL, 0, NULL, NULL);
	pCstr = (LPSTR)::HeapAlloc(::GetProcessHeap(), HEAP_ZERO_MEMORY, nChangeLen);
}

StringConversion::~StringConversion()
{
	if (bWstr)
		::HeapFree(::GetProcessHeap(), 0, pWstr);
	else
		::HeapFree(::GetProcessHeap(), 0, pCstr);
}

LPWSTR StringConversion::GetWstr()
{
	if (bWstr)
		::MultiByteToWideChar(nType, 0, pCstr, nLen, pWstr, nChangeLen);
	return pWstr;
}

LPSTR StringConversion::GetAstr()
{
	if (!bWstr)
		::WideCharToMultiByte(nType, 0, pWstr, nLen, pCstr, nChangeLen, NULL, NULL);
	return pCstr;
}

INT StringConversion::GetLength()
{
	return nChangeLen;
}

LPTSTR StringConversion::GetTstr()
{
#ifdef UNICODE
	return GetWstr();
#else
	return GetAstr();
#endif // UNICODE
}
